#include	"a_global_vars.h"
#include	"config_parser.h"

//------------------------------------------------------------------------------
int create_tcp_client_socket(const char *ip, int port) {
	struct sockaddr_in server_addr;
	int activate = 1;
	int e = -1;
	int sid;
	memset(&server_addr, '\0', sizeof(struct sockaddr_in));

	sid = socket(AF_INET, SOCK_STREAM, 0);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	while (e < 0) {
		e = connect(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
		if (e >= 0)
			break;
		close(sid);
		sleep(1);
		sid = socket(AF_INET, SOCK_STREAM, 0);
	}
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	return sid;
}
//------------------------------------------------------------------------------
int create_tcp_server_socket(const char *ip, int port) {

	struct sockaddr_in server_addr, client_addr;
	int sid, e, activate = 1;
	unsigned int client_addr_len;

	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, 1);

	printf("Server: PID: %d Waiting for connection requests from predecessor...\n", PROCESS_ID);
	int new_sid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
	setsockopt(new_sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	printf("connection request came from the node  on socket: %d\n", new_sid);

	return new_sid;
}
//------------------------------------------------------------------------------
void a_create_tcp_server_socket_4_learners(int port, const char *ip, int self_id) {

	int i = 0;
	struct sockaddr_in server_addr, client_addr;
	int sid, e, activate = 1;
	unsigned int client_addr_len;

	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, MAX_RING_CONNECTION);

	int ssid = 0;

	printf("Server: PID: %d Waiting for connection requests from learners...\n", PROCESS_ID);

	for (i = 0; i < get_config_info()->learner_tcp_sid_count; i++) {
		ssid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		char str[INET_ADDRSTRLEN];

		inet_ntop(AF_INET, &(client_addr.sin_addr.s_addr), str, INET_ADDRSTRLEN);
		int learner_id = find_learner_given_ip(get_config_info(), str);
		int l_index = find_learner_index_given_id(get_config_info(), learner_id, self_id);
		get_config_info()->server_infos[self_id].learner_tcp_acceptor[l_index].learner_tcp_sid = ssid;

		printf("connection request came from the node with port number:  %d	socket: %d\n", ntohs(client_addr.sin_port), get_config_info()->server_infos[self_id].learner_tcp_acceptor[l_index].learner_tcp_sid);

	}
}
